package com.xxx.common.base.util;

import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.alibaba.fastjson2.JSON;
import com.xxx.common.base.bo.UserTokenBO;

public class TokenUtil {

    private static final ThreadLocal<UserTokenBO> userThreadLocal = new ThreadLocal<>();

    public static UserTokenBO parseToken(String token) {
        JWT jwt = JWTUtil.parseToken(token);
        String user = (String) jwt.getPayload("user");
        return JSON.parseObject(user, UserTokenBO.class);
    }

    public static void setUserTokenBO(UserTokenBO userTokenBO) {
        userThreadLocal.set(userTokenBO);
    }

    public static UserTokenBO getUserTokenBO() {
        return userThreadLocal.get();
    }

    public static void clear() {
        userThreadLocal.remove();
    }

}
